##basic_distributions.r

pub.tab = t.test(dat$ingroup.public[is.na(dat$public.ingroup.preference)==F],dat$outgroup.public[is.na(dat$public.ingroup.preference)==F], paired = T)
dict.tab = t.test(dat$in.out.diff.dictator)
task.tab = t.test(dat$ingroup.task, dat$outgroup.task, paired = T) ##alternative is 0)
#task.tab = t.test(dat$ingroup.task, mu = .333) ##alternative is 1/3)

out.table = matrix(nrow = 3, ncol = 3)
out.table[1,1] = dict.tab$estimate; out.table[1,2] = dict.tab$statistic; out.table[1,3] = dict.tab$p.value
out.table[2,1] = pub.tab$estimate[1]; out.table[2,2] = pub.tab$statistic; out.table[2,3] = pub.tab$p.value
out.table[3,1] = task.tab$estimate[1]; out.table[3,2] = task.tab$statistic; out.table[3,3] = task.tab$p.value

out.table = round(out.table,3)
colnames(out.table) = c('mean','t statistic','p value')
rownames(out.table) = c('dictator','public goods', 'task')

x.out.table = xtable(out.table)

print(x.out.table, type = 'latex',file = 'output/Table_1.tex',
	floating = F)

	

###table of cooperation in public goods
###divide everyone and sometimes cooperators
dat.sometimes.coop = dat[is.na(dat$public.ingroup.preference)==F,]

##table
public.table = matrix(nrow = 3, ncol = 5)

##everyone
CC = nrow(dat[dat$ingroup.public == 1 & dat$outgroup.public == 1,])
DC = nrow(dat[dat$ingroup.public == 0 & dat$outgroup.public == 1,])
CD = nrow(dat[dat$ingroup.public == 1 & dat$outgroup.public == 0,])
DD = nrow(dat[dat$ingroup.public == 0 & dat$outgroup.public == 0,])

##fill the table
##raw numbers
public.table[1,1] = CC; public.table[2,1] = DC; public.table[1,3] = CD; public.table[2,3] = DD

##percents
CC.pct = nrow(dat[dat$ingroup.public == 1 & dat$outgroup.public == 1,])/nrow(dat)
DC.pct = nrow(dat[dat$ingroup.public == 0 & dat$outgroup.public == 1,])/nrow(dat)
CD.pct = nrow(dat[dat$ingroup.public == 1 & dat$outgroup.public == 0,])/nrow(dat)
DD.pct = nrow(dat[dat$ingroup.public == 0 & dat$outgroup.public == 0,])/nrow(dat)

public.table[1,2] = CC.pct; public.table[2,2] = DC.pct; public.table[1,4] = CD.pct; public.table[2,4] = DD.pct

##sums
public.table[3,1:4] = colSums(public.table[1:2,1:4])
public.table[1,5] = sum(public.table[1,c(2,4)])
public.table[2,5] = sum(public.table[2,c(2,4)])

public.table[,c(2,4,5)] = round(public.table[,c(2,4,5)],3)* 100 ##standardize

##create table with parentheses
out.table = cbind(paste(public.table[,1], ' (',public.table[,2],')', sep =''),
  paste(public.table[,3], ' (',public.table[,4],')', sep =''),
  paste('(',public.table[,5],')', sep ='')
  )

rownames(out.table) = c('Cooperate','Defect','')
colnames(out.table) = c('Cooperate','Defect','')

out.table = xtable(out.table)

print(out.table, type = 'latex',file = 'output/appendix/Table_A4.tex',
      floating = F,
      booktabs = T)


##################
##sometimes.coop
public.table = matrix(nrow = 3, ncol = 5)

CC = nrow(dat.sometimes.coop[dat.sometimes.coop$ingroup.public == 1 & dat.sometimes.coop$outgroup.public == 1,])
DC = nrow(dat.sometimes.coop[dat.sometimes.coop$ingroup.public == 0 & dat.sometimes.coop$outgroup.public == 1,])
CD = nrow(dat.sometimes.coop[dat.sometimes.coop$ingroup.public == 1 & dat.sometimes.coop$outgroup.public == 0,])
DD = nrow(dat.sometimes.coop[dat.sometimes.coop$ingroup.public == 0 & dat.sometimes.coop$outgroup.public == 0,])

##fill the table
##raw numbers
public.table[1,1] = CC; public.table[2,1] = DC; public.table[1,3] = CD; public.table[2,3] = DD

##percents
CC.pct = nrow(dat.sometimes.coop[dat.sometimes.coop$ingroup.public == 1 & dat.sometimes.coop$outgroup.public == 1,])/nrow(dat.sometimes.coop)
DC.pct = nrow(dat.sometimes.coop[dat.sometimes.coop$ingroup.public == 0 & dat.sometimes.coop$outgroup.public == 1,])/nrow(dat.sometimes.coop)
CD.pct = nrow(dat.sometimes.coop[dat.sometimes.coop$ingroup.public == 1 & dat.sometimes.coop$outgroup.public == 0,])/nrow(dat.sometimes.coop)
DD.pct = nrow(dat.sometimes.coop[dat.sometimes.coop$ingroup.public == 0 & dat.sometimes.coop$outgroup.public == 0,])/nrow(dat.sometimes.coop)

public.table[1,2] = CC.pct; public.table[2,2] = DC.pct; public.table[1,4] = CD.pct; public.table[2,4] = DD.pct

##sums
public.table[3,1:4] = colSums(public.table[1:2,1:4])
public.table[1,5] = sum(public.table[1,c(2,4)])
public.table[2,5] = sum(public.table[2,c(2,4)])

public.table[,c(2,4,5)] = round(public.table[,c(2,4,5)],3)* 100 ##standardize

##create table with parentheses
out.table = cbind(paste(public.table[,1], ' (',public.table[,2],')', sep =''),
                  paste(public.table[,3], ' (',public.table[,4],')', sep =''),
                  paste('(',public.table[,5],')', sep ='')
)

rownames(out.table) = c('Cooperate','Defect','')
colnames(out.table) = c('Cooperate','Defect','')

out.table = xtable(out.table)

print(out.table, type = 'latex',file = 'output/appendix/Table_A5.tex',
      floating = F,
      booktabs = T)
